package com.zhangxiaofanfan.hotcode.second;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-05-29 08:42:27
 */
public class HotCode035 {
    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(2);
        lruCache.put(1, 1);
        lruCache.put(2, 2);
        System.out.println(lruCache.get(1));
        lruCache.put(3, 3);
        System.out.println(lruCache.get(2));
    }
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */
class LRUCache {
    private Map<Integer, Integer> cache;
    private final Integer capacity;
    private Integer operationIndex = 0;
    private Map<Integer, Integer> keyOperationIndex;

    public LRUCache(int capacity) {
        this.cache = new HashMap<>();
        this.capacity = capacity;
        this.keyOperationIndex = new HashMap<>();
    }

    public int get(int key) {
        operationIndex++;
        if (keyOperationIndex.containsKey(key)) {
            keyOperationIndex.put(key, operationIndex);
        }
        return cache.getOrDefault(key, -1);
    }

    public void put(int key, int value) {
        operationIndex++;
        keyOperationIndex.put(key, operationIndex);
        this.cache.put(key, value);
    }
}
